package com.servlet;

import com.beans.AdminInfo;
import com.beans.MenuInfo;
import com.beans.PageInfo;
import com.beans.RoleInfo;
import com.dao.AdminDao;
import com.dao.MenuDao;
import com.dao.RoleDao;
import com.util.PageUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "RoleServlet.do", value = "/RoleServlet.do")
public class RoleServlet extends HttpServlet {
    private RoleDao roleDao = new RoleDao();
    private MenuDao menuDao = new MenuDao();
    private AdminDao adminDao = new AdminDao();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String flag=request.getParameter("flag");

        if ("manage".equals(flag)){
            manage(request,response);
        }else if ("roleMenu".equals(flag)){
            roleMenu(request,response);
        }else if ("updateRoleMenu".equals(flag)){
            updateRoleMenu(request,response);
        }else if ("add".equals(flag)){
            addRole(request,response);
        }else if ("deleteRole".equals(flag)){
            deleteRole(request,response);
        }else if ("update".equals(flag)){
            updateRole(request,response);
        }else if("listadmin".equals(flag)){
            listadmin(request,response);
        }else if ("assignment".equals(flag)){
            assignment(request,response);
        }else if ("doassignment".equals(flag)){
            doassignment(request,response);
        }else if ("checkRoleName".equals(flag)){
            checkRoleName(request,response);
        }
    }

    //验证角色名称是否存在
    private void checkRoleName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String roleName = request.getParameter("roleName");

        RoleInfo result = roleDao.checkRoleNameUsed(roleName);
        if (result == null){
            response.getWriter().print("1");
        }else {
            response.getWriter().print("0");
        }
    }

    //根据用户id修改用户角色id
    private void doassignment(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int id = Integer.parseInt(request.getParameter("id"));
        int roleId = Integer.parseInt(request.getParameter("roleId"));


        int result = adminDao.updateAdminRoleById(id,roleId);

        if (result==1){
            request.setAttribute("msg","<script>alert('用户修改成功')</script>");
            AdminInfo adminInfo = adminDao.getAdminInfoById(id);

            List<RoleInfo> roleList = roleDao.getRoleList();

            request.setAttribute("adminInfo",adminInfo);
            request.setAttribute("roleList",roleList);

            request.getRequestDispatcher("/perm/role_assignment.jsp").forward(request,response);
        }else{
            response.getWriter().print("用户修改失败");
        }
    }

    //通过传过来的用户id获取用户对象并转向
    private void assignment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("adminId"));
        AdminInfo adminInfo = adminDao.getAdminInfoById(id);

        List<RoleInfo> roleList = roleDao.getRoleList();

        request.setAttribute("adminInfo",adminInfo);
        request.setAttribute("roleList",roleList);

        request.getRequestDispatcher("/perm/role_assignment.jsp").forward(request,response);
    }

    private void listadmin(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        int pageSize = 10;
        int rowCount = adminDao.getAdminCount();
        int pageIndex = 1;
        String pageIndexstr = request.getParameter("pageIndex");
        if(pageIndexstr !=null) {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
            if(pageIndex>((rowCount+pageSize-1)/pageSize)) {
                pageIndex=((rowCount+pageSize-1)/pageSize);
            }
        }
        PageInfo page = PageUtil.getPageInfo(pageSize, rowCount, pageIndex);

        List<AdminInfo> adminList= adminDao.roleAdminList(page);
        request.setAttribute("adminList", adminList);
        request.setAttribute("page", page);

        request.getRequestDispatcher("/perm/role_listadmin.jsp").forward(request, response);
    }

    //修改用户角色
    private void updateRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String roleName = request.getParameter("roleName");
        String des = request.getParameter("des");
        int id = Integer.parseInt(request.getParameter("roleId"));

        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setRoleName(roleName);
        roleInfo.setDes(des);
        roleInfo.setId(id);

        int result = roleDao.updateRole(roleInfo);
        if (result==1){
            request.setAttribute("msg","用户修改成功");
        }else {
            request.setAttribute("msg","用户修改失败");
        }
        request.setAttribute("roleName",roleName);
        request.setAttribute("des",des);
        request.setAttribute("roleId",id);
        request.getRequestDispatcher("/perm/role_update.jsp").forward(request,response);
    }

    //删除角色
    private void deleteRole(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        int roleId = Integer.parseInt(request.getParameter("roleId"));

        RoleInfo checkResult = roleDao.checkRoleIdUsed(roleId);
        if (checkResult == null){
            int result = roleDao.deleteRoleById(roleId);
            if(result == 1){
                request.setAttribute("msg","<script>alert('删除角色成功')</script>");
                manage(request,response);
            }else{
                request.setAttribute("msg","<script>alert('删除失败')</script>");
            }
        }else{
            request.setAttribute("msg","<script>alert('该角色下有用户存在，不能删除')</script>");
            manage(request,response);
        }
    }

    //添加角色
    private void addRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String roleName = request.getParameter("roleName");
        String des = request.getParameter("des");

        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setRoleName(roleName);
        roleInfo.setDes(des);

        int result = roleDao.addRole(roleInfo);
        if (result==1){
            request.setAttribute("msg","用户添加成功");
        }else {
            request.setAttribute("msg","用户添加失败");
        }
        request.getRequestDispatcher("/perm/role_add.jsp").forward(request,response);
    }

    private void updateRoleMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        String [] menuIdList = request.getParameterValues("menuIds");
        int roleId = Integer.parseInt(request.getParameter("roleId"));

        roleDao.updateRoleMenu(roleId,menuIdList);
        request.setAttribute("msg","角色授权成功");

        roleMenu(request,response);
    }

    private void roleMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int roleId = Integer.parseInt(request.getParameter("roleId"));
        RoleInfo roleInfo = roleDao.getRoleInfoById(roleId);

        List<MenuInfo> menuList = menuDao.getMenuList(0);

        String menuIdStr=roleDao.getMenuIdStr(roleId);

        request.setAttribute("roleInfo",roleInfo);
        request.setAttribute("menuList",menuList);
        request.setAttribute("menuIdStr",menuIdStr);

        request.getRequestDispatcher("/perm/role-menu.jsp").forward(request,response);
    }

    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<RoleInfo> roleList = roleDao.getRoleList();
        request.setAttribute("roleList",roleList);
        request.getRequestDispatcher("/perm/role-manage.jsp").forward(request,response);
    }


}
